﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;

public class XTJ100GameOver : MonoBehaviour
{

    EginXTJ xtj = EginXTJ.Instance;
    public List<GameObject> ScoreSprites;
    public List<GameObject> ScoreFathers;
    public List<UISprite> JiaJians;
    public PaiDealCardBroadcast newpb;
    public List<XTJUser> xtjUsers;
    public GameObject gameOverPanel;

    private XTJMyOwnInfo xtjMyOwnInfo;
    private XTJBankerInfo xtjBankerInfo;
    private GameObject cardsPanel;
    private XTJGame100 xtjGame100;

    public AudioClip victoryClip;//胜利音效
    public AudioClip defeatClip;//失败音效

    void Start()
    {

        xtjGame100 = GameObject.Find("UI Root/GameXTJ100Panel").GetComponent<XTJGame100>();
        cardsPanel = GameObject.Find("UI Root/CardsPanel");
        xtjMyOwnInfo = GameObject.Find("UI Root/GameXTJ100Panel/Bottom/MyOwnInfo").GetComponent<XTJMyOwnInfo>();
        xtjBankerInfo = GameObject.Find("UI Root/GameXTJ100Panel/Top/BankerInfo").GetComponent<XTJBankerInfo>();
        for (int i = 0; i < 4; i++)
        {
            xtjUsers[i] = GameObject.Find("UI Root/GameXTJ100Panel/Players/Player" + i).GetComponent<XTJUser>();
        }

        if (newpb != null)
        {
            //for (int i = 0; i < newpb.CoinMap.Count; i++)
            {
                foreach (KeyValuePair<int, PaiInt64Array> keyValue in newpb.CoinMap)
                {
                    Debug.Log("Id:" + keyValue.Key + "庄家Id:" + xtj.bankerId);
                    //显示自己的得分
                    if (keyValue.Key == int.Parse(EginUser.Instance.Uid))
                    {
                        ShowScore(keyValue.Value.Value[0], ScoreSprites[4], ScoreFathers[4], JiaJians[4]);
                        //显示自己剩余的金币数
                        xtjMyOwnInfo.goldLabel.text = EginUser.Instance.ReturnMoneyForLong(keyValue.Value.Value[1]);
                        //更新自己的金币数
                        EginUser.Instance.coinCount = keyValue.Value.Value[1].ToString();
                        if (keyValue.Value.Value[0] >= 0)
                        {
                            //播放胜利的音效
                            SoundModel.PlaySound(victoryClip);
                        }
                        else
                        {
                            //播放失败的音效
                            SoundModel.PlaySound(defeatClip);
                        }
                    }
                    if (keyValue.Key == xtj.bankerId)
                    {
                        Debug.Log("显示庄家的得分");
                        ShowScore(keyValue.Value.Value[0], ScoreSprites[5], ScoreFathers[5], JiaJians[5]);
                        //更新庄家的金币数
                        xtjBankerInfo.goldLabel.text = EginUser.Instance.ReturnMoneyForLong(keyValue.Value.Value[1]);
                    }
                    for (int j = 0; j < 4; j++)
                    {
                        if (keyValue.Key == xtjUsers[j].uid)
                        {
                            ShowScore(keyValue.Value.Value[0], ScoreSprites[j], ScoreFathers[j], JiaJians[j]);
                            //更新上座的人的金币数
                            xtjUsers[j].goldLabel.text = EginUser.Instance.ReturnMoneyForLong(keyValue.Value.Value[1]);
                        }
                    }
                }
                //奖池更新
                GameObject.Find("UI Root/GameXTJ100Panel/Top/JiangChi").GetComponent<UILabel>().text = newpb.BonusPoolCoin.ToString();



            }
        }


        StartCoroutine(DestroyGameObect());
    }


    /// <summary>
    /// 显示加减的分数
    /// </summary>
    public void ShowScore(long score, GameObject obj1, GameObject obj1Father, UISprite sprite)
    {
        string plusLess = "";
        float x = obj1.transform.localPosition.x;
        float y = obj1.transform.localPosition.y;

        string tempStr = "";

        //分数大于0
        if (score >= 0)
        {
            plusLess = "plus_";
            //显示+
            sprite.gameObject.SetActive(true);
            sprite.spriteName = "plus_" + "jia";
            tempStr = EginUser.Instance.ReturnMoneyForLong(score);
        }
        else
        {
            plusLess = "less_";
            //显示-
            sprite.gameObject.SetActive(true);
            sprite.spriteName = "less_" + "jian";
            tempStr = EginUser.Instance.ReturnMoneyForLong(long.Parse(score.ToString().Remove(0, 1)));
        }

        int len = tempStr.Length;

        //设置分数
        for (int i = 0; i < len; i++)
        {
            GameObject obj = Instantiate(obj1);
            obj.transform.parent = obj1Father.transform;
            obj.transform.localScale = Vector3.one;
            obj.SetActive(true);
            string str = tempStr.Substring(0, 1);
            //Debug.Log(str);
            if (i == 0)
            {
                obj.transform.localPosition = new Vector3(x, y, 0);
            }
            else
            {
                if (str == ".")
                {
                    x += 26;

                }
                else if (str == "万")
                {
                    x += 50;
                    str = "w";
                }
                else if (str == "亿")
                {
                    x += 50;
                    str = "yi";
                }
                else
                {
                    x += 40;
                }
                obj.transform.localPosition = new Vector3(x, y, 0);
            }
            tempStr = tempStr.Remove(0, 1);
            obj.GetComponent<UISprite>().spriteName = plusLess + str;

        }
    }


    IEnumerator DestroyGameObect()
    {
        yield return new WaitForSeconds(4f);
        GameObject freeTime = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)Resources.Load(GameXTJ100PrefabPath.FreeTime));
        freeTime.transform.localPosition = new Vector3(-35, 8, 0);
        if (xtjGame100 != null)
        {
            xtjGame100.RecoverInit();
        }

        if (cardsPanel != null)
        {
            Destroy(cardsPanel);
        }

        Destroy(gameOverPanel);
    }

}
